﻿using BaseUi.Models;
using BaseUi.ViewModels;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace BaseUi
{
    /// <summary>
    /// ElectricalDeviceView.xaml 的交互逻辑
    /// </summary>
    public partial class ElectricalDeviceView : UserControlBase
    {
        private RunState runState = RunState.Stop;
        /// <summary>
        /// 运行状态
        /// </summary>
        CancellationTokenSource cancellationToken1 = null;//停止
        ManualResetEvent manualReset1 = null;//暂停、继续
        /// <summary>
        /// 开始
        /// </summary>
        public void Start()
        {
            cancellationToken1 = new CancellationTokenSource();
            manualReset1 = new ManualResetEvent(true);
            runState = RunState.Run;
            //Task.Run(TaskAll);
        }/// <summary>
         /// 停止
         /// </summary>
        public void Stop()
        {
            cancellationToken1.Cancel();
            runState = RunState.Stop;
        }
        //private DispatcherTimer timer;
        //设置定时器          
        ElectricalDeviceViewModel viewModel = new ElectricalDeviceViewModel();
        public ElectricalDeviceView()
        {
            //timer = new DispatcherTimer();
            //timer.Interval = new TimeSpan(10000000);   //时间间隔为一秒
            //timer.Tick += new EventHandler(timer_Tick);
            //转换成秒数
            //Int32 hour = Convert.ToInt32(HourArea.Text);
            //Int32 minute = Convert.ToInt32(MinuteArea.Text);
            //Int32 second = Convert.ToInt32(SecondArea.Text);
            //开启定时器
            //timer.Stop();
            //timer.Start();
            InitializeComponent();
            
            this.DataContext = viewModel;
        }

        

        /// <summary>
        /// 气缸状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void zt1(object sender, RoutedEventArgs e)
        {
            if (HomeView.viewModel.RunState == RunState.Run)
            {
                textBoxInfo.AppendText($"处于运行模式禁止调试\r\n");
            }
            if (HomeView.viewModel.RunState == RunState.Pause)
            {
                textBoxInfo.AppendText($"处于暂停模式禁止调试\r\n");
            }
             try
            {
                var aaa = LocalStatic.Electric.Readsentfrequency(500, PortModel.Accurate, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"成功\r\n");
                    textBoxInfo.AppendText($"成功"+ aaa.Value+ "\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 顶升状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void zt2(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentfrequency(500, PortModel.Jacking, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"成功\r\n");
                    textBoxInfo.AppendText($"成功" + aaa.Value + "\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 按压状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void zt3(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentfrequency(500, PortModel.Pressing, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"成功\r\n");
                    textBoxInfo.AppendText($"成功" + aaa.Value + "\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        ///正向旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz1(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = sfsafe();
                if (retul!="OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate1, PortModel.Rotate2, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"正向旋转成功\r\n");
                    HomeView.viewModel.way = 0;
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        private string sfsafe() {
            var ioi = LocalStatic.Electric.Readsentfrequency(500, PortModel.EAll, LocalFile.Config.Address);
            if (!ioi.IsSucceed)
            {
                return ioi.Err;
            }
            else
            {

                string[] rest = ioi.Value.Split(' ');

                //var send = SetTrigger( LocalFile.Config.Address);
                if (ioi.Value.Length != 42)
                {
                    return "长度错误";
                }
                if (rest[2] != "01")
                {
                    return "顶升1-1顶断开未检测到位" + rest[2];
                }

                if (rest[3] != "00")
                {
                    return "顶升1-2顶闭合异常" + rest[3];
                }
                if (rest[4] != "01")
                {
                    return "顶升2-1顶断开未检测到位" + rest[4];
                }
                if (rest[5] != "00")
                {
                    return "顶升2-2顶闭合异常" + rest[5];
                }

                if (rest[6] != "00")
                {
                    //return "开关箱报警";
                }
                if (rest[8] != "00")
                {
                    return "急停按钮报警";
                }
                if (rest[9] != "00")
                {
                    //return "光栅报警";
                }
            }
            return "OK";
        }
        /// <summary>
        ///反向旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz2(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = sfsafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate2, PortModel.Rotate1, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"反向旋转成功\r\n");
                    HomeView.viewModel.way = 0;
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将旋转气缸全部关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz3(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Accurate, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸顶升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ds(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Jack, new List<int>(), LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"顶升气缸成功顶升\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸顶升断电
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dsdk(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"顶升气缸成功下降\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
         /// 将气缸按压
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>

        private void ay(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Suppress, new List<int>(), LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"按压气缸成功按压\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
         /// 将气缸按压断电
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
        private void aydk(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Suppress, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"按压气缸成功断开\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 1号led灯OK亮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void led1ok(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"1号红灯熄灭通信失败\r\n");
                }
                aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 1, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"1号绿灯点亮通信失败\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"1号绿灯成功亮起\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 1号led灯NG亮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void led1ng(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"1号绿灯熄灭通信失败\r\n");
                }
                aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 1, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"1号红灯点亮通信失败\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"1号红灯成功亮起\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void led1nok(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"1号绿灯熄灭成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        private void led1nng(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"1号红灯熄灭成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void led2ok(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"2号红灯熄灭通信失败\r\n");
                }
                aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 1, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"2号绿灯点亮通信失败\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"2号绿灯成功亮起\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void led2ng(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"2号绿灯熄灭通信失败\r\n");
                }
                aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 1, LocalFile.Config.Address);
                if (!aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"2号红灯点亮通信失败\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"2号红灯成功亮起\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void led2nok(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500,PortModel.LED2ok, 0, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"2号绿灯熄灭成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void led2nng(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"2号红灯熄灭成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        
        /// <summary>
        /// 状态查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void renovate1()
        {
            while (true)
            {
                Thread.Sleep(500);
                While.Wait(cancellationToken1.Token, manualReset1);
                var aaa = LocalStatic.Electric.Readsentfrequency(500, PortModel.EAll, LocalFile.Config.Address);

                if (aaa.IsSucceed)
                {
                    string[] rest = aaa.Value.Split(' ');

                    //var send = SetTrigger( LocalFile.Config.Address);
                    if (aaa.Value.Length != 42)
                    {
                        textBoxInfo.AppendText("长度错误");
                        return;
                    }
                    if (rest[0] != "01")
                    {
                        viewModel.condition1 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition1 = false;
                    }
                    if (rest[1] != "01")
                    {
                        viewModel.condition2 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition2 = false;
                    }
                    if (rest[2] != "01")
                    {
                        viewModel.condition3 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition3 = false;
                    }
                    if (rest[3] != "01")
                    {
                        viewModel.condition4 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition4 = false;
                    }
                    if (rest[4] != "01")
                    {
                        viewModel.condition5 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition5 = false;
                    }
                    if (rest[5] != "01")
                    {
                        viewModel.condition6 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition6 = false;
                    }
                    if (rest[6] != "01")
                    {
                        viewModel.condition7 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition7 = false;
                    }
                    if (rest[8] != "01")
                    {
                        viewModel.condition8 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition8 = false;
                    }
                    if (rest[9] != "01")
                    {
                        viewModel.condition9 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition9 = false;
                    }
                    if (rest[10] != "01")
                    {
                        viewModel.condition10 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition10 = false;
                    }
                    if (rest[11] != "01")
                    {
                        viewModel.condition11 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition11 = false;
                    }
                    if (rest[12] != "01")
                    {
                        viewModel.condition12 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition12 = false;
                    }
                    if (rest[13] != "01")
                    {
                        viewModel.condition13 = true;
                        //return;
                    }
                    else
                    {
                        viewModel.condition13 = false;
                    }
                }
                else
                {
                    viewModel.condition1 = false;
                    viewModel.condition2 = false;
                    viewModel.condition3 = false;
                    viewModel.condition4 = false;
                    viewModel.condition5 = false;
                    viewModel.condition6 = false;
                    viewModel.condition7 = false;
                    viewModel.condition8 = false;
                    viewModel.condition9 = false;
                    viewModel.condition10 = false;
                    viewModel.condition11 = false;
                    viewModel.condition12 = false;
                    viewModel.condition13 = false;
                    //textBoxInfo.AppendText("电气通信失败\r\n");
                    viewModel.text = viewModel.text + ("电气通信失败\r\n");
                }
                for (int i = 0; i < 4; i++)
                {
                    bool have = false;
                    bool start = false;
                    //string num = (i+1)+"号测试位";
                    var gettext = LocalStatic.Test[0 + i].ReadSensor();
                    if (!gettext.IsSucceed)
                    {
                        //num = num+"异常";
                        //textBoxInfo.AppendText((i + 1).ToString() + "号测试箱通信失败！\r\n");
                        viewModel.text = viewModel.text+ ((i + 1).ToString() + "号测试箱通信失败！\r\n");
                    }
                    else
                    {
                        int getnumber = int.Parse(gettext.Value);
                        if (getnumber < LocalFile.Config.DSensor[0 + i])
                        {
                            start = true;
                        }
                        if (getnumber < LocalFile.Config.USensor[0 + i])
                        {
                            have = true;
                        }
                    }
                    if (i == 0)
                    {
                        viewModel.ganyihave1 = have;
                        viewModel.ganyistart1 = start;
                    }
                    else if (i == 1)
                    {
                        viewModel.ganyihave2 = have;
                        viewModel.ganyistart2 = start;
                    }
                    else if (i == 2)
                    {
                        viewModel.ganyihave3 = have;
                        viewModel.ganyistart3 = start;

                    }
                    else if (i == 3)
                    {
                        viewModel.ganyihave4 = have;
                        viewModel.ganyistart4 = start;

                    }
                }
            }
        }

        private void renovate(object sender, RoutedEventArgs e)
        {

            if (runState == RunState.Stop)
            {
                Start();
                textBoxInfo.AppendText("开始刷新状态检测");
                Task.Run(renovate1);
            }
            else
            {
                textBoxInfo.AppendText("正在状态检测");
            }
        }

        private void renovatend(object sender, RoutedEventArgs e)
        {
            Stop();
            textBoxInfo.AppendText("停止状态检测");
        }
    }
}
